{
	template:`
    <m-page @back="back" title="薪资计发">
        <template #nav>
            <div class="nav text-center bg-gray">
                <div v-for="ym in yearMonth" class="cu-item" :class="ym==selectIndex?'text-blue cur':''" @click="selectItem(ym)">{{ym}}</div>
            </div>
        </template>
        <template #bar>
            <div class="flex-sub padding-lr text-right">
                本月薪资合计：{{formatAmount(allWagesAmount)}}元
            </div>
            <div class="m-buttons padding-right">
                <m-button @click="countAll">一键计算</m-button>
            </div>
        </template>
        <m-top></m-top>
        <m-card join v-for="item in wagesList">
            <div class="flex padding-sm align-center">
                <div class="cu-tag radius" :class="'bg-'+item.user.store.color">{{item.user.store.name}}</div>
                <div class="flex-sub padding-lr-sm">
                    <span>{{item.user.realName}}</span>
                    <span class="text-sm text-gray padding-left-xs">{{item.user.phoneNum}}</span>
                </div>
                <div v-if="item.user.status=='Y'" class="text-right">
                    <div v-if="item.user.powers.indexOf('A')>=0" class="cu-tag radius line-blue sm">管理</div>
                    <div v-if="item.user.powers.indexOf('B')>=0" class="cu-tag radius line-green sm">店长</div>
                </div>
                <status v-else :value="item.user.status"/>
            </div>
            <div v-if="item.oid" class="flex align-center padding-lr-sm padding-bottom-sm">
                <div v-if="item.status!='Y'" class="padding-right-xs" @click="countUser(item)">
                    <m-icon name="shebeishengmingzhouqi" size="26" class="text-blue">重算</m-icon>
                </div>
                <div class="flex-sub text-sm">本月薪资：{{formatAmount(item.payAmount)}}元</div>
                <div v-if="item.status=='Y'" class="text-xs text-right">
                    <div class="text-sm">{{item.confirmUser.realName}}已发放</div>
                    <div>{{formatDate(item.confirmDate,'yyyy-MM-dd')}}</div>
                </div>
                <div>
                    <m-button size="sm" line @click="openDetail(item.oid)" class="margin-left-xs">查看明细</m-button>
                    <m-button v-if="item.status!='Y'&&item.payAmount>0" size="sm" type="success" class="margin-left-xs" @click="confirmWages(item.oid)">确认发放</m-button>
                </div>
            </div>
            <div v-else class="padding-lr-sm padding-bottom-sm text-right">
                <m-button size="sm" @click="countUser(item)">立即计算</m-button>
            </div>
        </m-card>
    </m-page>
	`,
	props: ['param','parentVue'],
	data:function(){
		return {
		    selectIndex:'',
		    yearMonth:[],
		    userList:[],
            wagesList:[],
            allWagesAmount:0.0
		};
	},
	mounted(){
        this.requestData('action/order/monthWages/getWagesUser',
            {},
            (data)=>{
                if(data.code==0){
                    this.userList=data.userList;
                    //初始化参数
                    let y=new Date().getFullYear();
                    let m=new Date().getMonth();
                    this.selectItem(y+"年"+m+"月");
                    for(let i=0;i<3;i++){
                        if(m==0){
                            m=12;y=y-1;
                        }
                        this.yearMonth.push(y+"年"+m+"月");
                        m=m-1;
                    }
                }else{
                    this.remind().error(data.msg);
                }
            }
        );
	},
	methods:{
	    selectItem:function(y){
	        this.selectIndex=y;
	        this.loadList();
	    },
	    loadList:function(){
            this.requestData('action/order/monthWages/getWagesList',
                {yearMonth:this.selectIndex},
                (data)=>{
                    if(data.code==0){
                        this.wagesList=data.wagesList;
                        this.allWagesAmount=0.0;
                        for(let j=0;j<this.wagesList.length;j++){
                            this.allWagesAmount+=this.wagesList[j].payAmount||0.0;
                        }
                        for(let i=0;i<this.userList.length;i++){
                            let user=this.userList[i];
                            let add=true;
                            for(let j=0;j<this.wagesList.length;j++){
                                let w=this.wagesList[j];
                                if(w.user&&w.user.oid==user.oid){
                                    add=false;
                                    break;
                                }
                            }
                            if(add){
                                this.wagesList.push({user:user});
                            }
                        }
                    }else{
                        this.remind().error(data.msg);
                    }
                }
            );
	    },
	    countUser:function(item,backCall){
	        let fn=()=>{
                this.requestData('action/order/monthWages/count',
                    {userOid:item.user.oid,yearMonth:this.selectIndex},
                    (data)=>{
                        if(backCall){
                            backCall();
                        }else{
                            if(data.code==0){
                                this.remind().success(data.msg);
                                this.loadList();
                            }else{
                                this.remind().error(data.msg);
                            }
                        }
                    }
                );
	        };
	        if(backCall){
                fn();
	        }else{
                this.confirm("计算确认","确定要计算该员工的薪资吗？",fn);
	        }
	    },
	    countAll:function(){
            let fn=(i)=>{
                if(this.wagesList.length>i){
                    this.remind().success("计算中（"+(i+1)+"/"+this.wagesList.length+"）");
                    this.countUser(this.wagesList[i],()=>{
                        fn(i+1);
                    });
                }else{
                    this.remind().success("全部计算完成。");
                    this.loadList();
                }
            };
            this.confirm("计算确认","确定要计算全部员工的薪资吗？",()=>{
                fn(0);
            });
	    },
	    confirmWages:function(oid){
            this.confirm("发放确认","确定该员工的薪资已发放？",()=>{
                this.requestData('action/order/monthWages/confirm',
                    {oid:oid},
                    (data)=>{
                        if(data.code==0){
                            this.remind().success(data.msg);
                            this.loadList();
                        }else{
                            this.remind().error(data.msg);
                        }
                    }
                );
            });
	    },
	    openDetail:function(oid){
	        this.openPage('order/wages/monthWagesDetailList.js',{oid:oid},(flag,obj)=>{
	            if(flag){}
	        });
	    }
	}
}